﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Package_Entity : EntityBase<PACKAGE>
    {
        public override RESP_Info Add(PACKAGE model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into PACKAGE(PACKAGE_ID,PACKAGE_DES,PACKAGE_INCLUDE,PACKAGE_EXCLUDE) values (@PACKAGE_ID,@PACKAGE_DES,@PACKAGE_INCLUDE,@PACKAGE_EXCLUDE)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("PACKAGE_ID", model.PACKAGE_ID));
                    cmd.Parameters.Add(new SqlParameter("PACKAGE_DES", model.PACKAGE_DES));
                    cmd.Parameters.Add(new SqlParameter("PACKAGE_INCLUDE", model.PACKAGE_INCLUDE));
                    cmd.Parameters.Add(new SqlParameter("PACKAGE_EXCLUDE", model.PACKAGE_EXCLUDE));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加旅程套餐成功</p></h5>";
                    info.Url = "RouteList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加旅程套餐失败</p></h5>";
                    info.Url = "PackageAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加旅程套餐失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(PACKAGE model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update PACKAGE set PACKAGE_DES=@PACKAGE_DES,PACKAGE_INCLUDE=@PACKAGE_INCLUDE,PACKAGE_EXCLUDE=@PACKAGE_EXCLUDE where PACKAGE_ID=@PACKAGE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("PACKAGE_ID", model.PACKAGE_ID));
                    cmd.Parameters.Add(new SqlParameter("PACKAGE_DES", model.PACKAGE_DES));
                    cmd.Parameters.Add(new SqlParameter("PACKAGE_INCLUDE", model.PACKAGE_INCLUDE));
                    cmd.Parameters.Add(new SqlParameter("PACKAGE_EXCLUDE", model.PACKAGE_EXCLUDE));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑旅程套餐成功</p></h5>";
                    info.Url = "RouteList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑旅程套餐失败</p></h5>";
                    info.Url = "RouteList.aspx";

                    Tom.Net.Log.LogManager.Error("编辑旅程套餐失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(PACKAGE routePackage)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from PACKAGE where PACKAGE_ID=@PACKAGE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("PACKAGE_ID", routePackage.PACKAGE_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除旅程套餐成功</p></h5>";
                    info.Url = "RouteList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除旅程套餐失败</p></h5>";
                    info.Url = "RouteList.aspx";

                    Tom.Net.Log.LogManager.Error("删除旅程套餐失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override PACKAGE GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select PACKAGE_ID,PACKAGE_DES,PACKAGE_INCLUDE,PACKAGE_EXCLUDE from PACKAGE where PACKAGE_ID=@PACKAGE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("PACKAGE_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        PACKAGE routePackage = new PACKAGE();
                        if (!reader.IsDBNull(0)) routePackage.PACKAGE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routePackage.PACKAGE_DES = reader.GetString(1);
                        if (!reader.IsDBNull(2)) routePackage.PACKAGE_INCLUDE = reader.GetString(2);
                        if (!reader.IsDBNull(3)) routePackage.PACKAGE_EXCLUDE = reader.GetString(3);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routePackage;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程套餐数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override PACKAGE GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        PACKAGE routePackage = new PACKAGE();
                        if (!reader.IsDBNull(0)) routePackage.PACKAGE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routePackage.PACKAGE_DES = reader.GetString(1);
                        if (!reader.IsDBNull(2)) routePackage.PACKAGE_INCLUDE = reader.GetString(2);
                        if (!reader.IsDBNull(3)) routePackage.PACKAGE_EXCLUDE = reader.GetString(3);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routePackage;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程套餐数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<PACKAGE> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<PACKAGE> routePackages = new List<PACKAGE>();
                    while (reader.Read())
                    {
                        PACKAGE routePackage = new PACKAGE();
                        if (!reader.IsDBNull(0)) routePackage.PACKAGE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routePackage.PACKAGE_DES = reader.GetString(1);
                        if (!reader.IsDBNull(2)) routePackage.PACKAGE_INCLUDE = reader.GetString(2);
                        if (!reader.IsDBNull(3)) routePackage.PACKAGE_EXCLUDE = reader.GetString(3);

                        routePackages.Add(routePackage);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return routePackages;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程套餐数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
